package com.uft.model.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.uft.model.entity.Users;

@Repository
public class UsersDAO {
	@PersistenceContext private EntityManager em;
	
	
	public List<Users> getUser(String username, String password) {
	       TypedQuery<Users> query = em.createQuery(
	           "SELECT u FROM Users u where u.username=\'"+username+"\' and u.password=\'"+password+"\'", Users.class);
	       return query.getResultList();
	}
	public Users getUserByName(String username) {
	       TypedQuery<Users> query = em.createQuery(
	           "SELECT u FROM Users u where u.username=\'"+username+"\'", Users.class);
	       return query.getSingleResult();
	}
	
	public List<Users> getUserList(){
		 TypedQuery<Users> query = em.createQuery(
		           "SELECT u FROM Users u", Users.class);
		 return query.getResultList();
	}
	
	@Transactional
	public void addUser(Users user){
	   System.out.println("User Name : "+user.getUsername());
	   em.persist(user);
	   System.out.println("user id: "+user.getUserid());
	}
	
	@Transactional
	public void updateUser(Users user) {
		// TODO Auto-generated method stub
	   System.out.println("username name: "+user.getUsername());
	   em.merge(user);
	   System.out.println("user id: "+user.getUserid());
		
	}
   @Transactional
   public void deleteUserById(int userid) {
	// TODO Auto-generated method stub
	   System.out.println("user id: "+userid);
	   Users user = em.find(Users.class, userid);
	   em.remove(user);
	   
   }
}
